#region Copyright © 2004, Nicholas Berardi
/*
 * ManagedFusion (www.ManagedFusion.net) Copyright © 2004, Nicholas Berardi
 * All rights reserved.
 * 
 * This code is protected under the Common Public License Version 1.0
 * The license in its entirety at <http://opensource.org/licenses/cpl.php>
 * 
 * ManagedFusion is freely available from <http://www.ManagedFusion.net/>
 */
#endregion

///////////////////////////////////////////////////////////////////////////
// Description: Data Access class for the table 'Blog_Posts'
// Generated by LLBLGen v1.21.2003.712 Final on: Wednesday, January 26, 2005, 4:47:11 PM
// Because the Base Class already implements IDispose, this class doesn't.
///////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace OmniPortal.Modules.Blog.Data.SqlServer
{
	/// <summary>
	/// Purpose: Data Access class for the table 'Blog_Posts'.
	/// </summary>
	public class Blog_Posts : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlBoolean		_published, _allowComments, _syndicate;
			private SqlDateTime		_modified, _created, _issued;
			private SqlInt32		_iD;
			private SqlString		_sourceUrl, _source, _body, _title, _titleUrl;
			private SqlGuid			_user_ID;
		#endregion


		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public Blog_Posts()
		{
			// Nothing for now.
		}


		/// <summary>
		/// Purpose: Insert method. This method will insert one new row into the database.
		/// </summary>
		/// <returns>True if succeeded, otherwise an Exception is thrown. </returns>
		/// <remarks>
		/// Properties needed for this method: 
		/// <UL>
		///		 <LI>Title</LI>
		///		 <LI>Body</LI>
		///		 <LI>Published</LI>
		///		 <LI>AllowComments</LI>
		///		 <LI>Syndicate</LI>
		///		 <LI>User_ID</LI>
		///		 <LI>TitleUrl. May be SqlString.Null</LI>
		///		 <LI>Source. May be SqlString.Null</LI>
		///		 <LI>SourceUrl. May be SqlString.Null</LI>
		///		 <LI>Created</LI>
		///		 <LI>Issued</LI>
		///		 <LI>Modified</LI>
		/// </UL>
		/// Properties set after a succesful call of this method: 
		/// <UL>
		///		 <LI>Identity</LI>
		///		 <LI>ErrorCode</LI>
		/// </UL>
		/// </remarks>
		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[Blog_Posts_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@Title", SqlDbType.NVarChar, 128, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _title));
				cmdToExecute.Parameters.Add(new SqlParameter("@Body", SqlDbType.NText, _body.Value.Length, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _body));
				cmdToExecute.Parameters.Add(new SqlParameter("@Published", SqlDbType.Bit, 1, ParameterDirection.Input, false, 1, 0, "", DataRowVersion.Proposed, _published));
				cmdToExecute.Parameters.Add(new SqlParameter("@AllowComments", SqlDbType.Bit, 1, ParameterDirection.Input, false, 1, 0, "", DataRowVersion.Proposed, _allowComments));
				cmdToExecute.Parameters.Add(new SqlParameter("@Syndicate", SqlDbType.Bit, 1, ParameterDirection.Input, false, 1, 0, "", DataRowVersion.Proposed, _syndicate));
				cmdToExecute.Parameters.Add(new SqlParameter("@User_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _user_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@TitleUrl", SqlDbType.NVarChar, 2048, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _titleUrl));
				cmdToExecute.Parameters.Add(new SqlParameter("@Source", SqlDbType.NVarChar, 128, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _source));
				cmdToExecute.Parameters.Add(new SqlParameter("@SourceUrl", SqlDbType.NVarChar, 2048, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _sourceUrl));
				cmdToExecute.Parameters.Add(new SqlParameter("@Created", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 23, 3, "", DataRowVersion.Proposed, _created));
				cmdToExecute.Parameters.Add(new SqlParameter("@Issued", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 23, 3, "", DataRowVersion.Proposed, _issued));
				cmdToExecute.Parameters.Add(new SqlParameter("@Modified", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 23, 3, "", DataRowVersion.Proposed, _modified));
				cmdToExecute.Parameters.Add(new SqlParameter("@Identity", SqlDbType.Int, 4, ParameterDirection.Output, true, 10, 0, "", DataRowVersion.Proposed, _iD));
				cmdToExecute.Parameters.Add(new SqlParameter("@ErrorCode", SqlDbType.Int, 4, ParameterDirection.Output, true, 10, 0, "", DataRowVersion.Proposed, _errorCode));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				_iD = (Int32)cmdToExecute.Parameters["@Identity"].Value;
				_errorCode = (Int32)cmdToExecute.Parameters["@ErrorCode"].Value;

				if(_errorCode != (int)LLBLError.AllOk)
				{
					// Throw error.
					throw new Exception("Stored Procedure 'Blog_Posts_Insert' reported the ErrorCode: " + _errorCode);
				}

				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Blog_Posts::Insert::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		/// <summary>
		/// Purpose: Update method. This method will Update one existing row in the database.
		/// </summary>
		/// <returns>True if succeeded, otherwise an Exception is thrown. </returns>
		/// <remarks>
		/// Properties needed for this method: 
		/// <UL>
		///		 <LI>Identity</LI>
		///		 <LI>Title</LI>
		///		 <LI>Body</LI>
		///		 <LI>Published</LI>
		///		 <LI>AllowComments</LI>
		///		 <LI>Syndicate</LI>
		///		 <LI>User_ID</LI>
		///		 <LI>TitleUrl. May be SqlString.Null</LI>
		///		 <LI>Source. May be SqlString.Null</LI>
		///		 <LI>SourceUrl. May be SqlString.Null</LI>
		///		 <LI>Created</LI>
		///		 <LI>Issued</LI>
		///		 <LI>Modified</LI>
		/// </UL>
		/// Properties set after a succesful call of this method: 
		/// <UL>
		///		 <LI>ErrorCode</LI>
		/// </UL>
		/// </remarks>
		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[Blog_Posts_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@Identity", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _iD));
				cmdToExecute.Parameters.Add(new SqlParameter("@Title", SqlDbType.NVarChar, 128, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _title));
				cmdToExecute.Parameters.Add(new SqlParameter("@Body", SqlDbType.NText, _body.Value.Length, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _body));
				cmdToExecute.Parameters.Add(new SqlParameter("@Published", SqlDbType.Bit, 1, ParameterDirection.Input, false, 1, 0, "", DataRowVersion.Proposed, _published));
				cmdToExecute.Parameters.Add(new SqlParameter("@AllowComments", SqlDbType.Bit, 1, ParameterDirection.Input, false, 1, 0, "", DataRowVersion.Proposed, _allowComments));
				cmdToExecute.Parameters.Add(new SqlParameter("@Syndicate", SqlDbType.Bit, 1, ParameterDirection.Input, false, 1, 0, "", DataRowVersion.Proposed, _syndicate));
				cmdToExecute.Parameters.Add(new SqlParameter("@User_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _user_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@TitleUrl", SqlDbType.NVarChar, 2048, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _titleUrl));
				cmdToExecute.Parameters.Add(new SqlParameter("@Source", SqlDbType.NVarChar, 128, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _source));
				cmdToExecute.Parameters.Add(new SqlParameter("@SourceUrl", SqlDbType.NVarChar, 2048, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _sourceUrl));
				cmdToExecute.Parameters.Add(new SqlParameter("@Created", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 23, 3, "", DataRowVersion.Proposed, _created));
				cmdToExecute.Parameters.Add(new SqlParameter("@Issued", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 23, 3, "", DataRowVersion.Proposed, _issued));
				cmdToExecute.Parameters.Add(new SqlParameter("@Modified", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 23, 3, "", DataRowVersion.Proposed, _modified));
				cmdToExecute.Parameters.Add(new SqlParameter("@ErrorCode", SqlDbType.Int, 4, ParameterDirection.Output, true, 10, 0, "", DataRowVersion.Proposed, _errorCode));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				_errorCode = (Int32)cmdToExecute.Parameters["@ErrorCode"].Value;

				if(_errorCode != (int)LLBLError.AllOk)
				{
					// Throw error.
					throw new Exception("Stored Procedure 'Blog_Posts_Update' reported the ErrorCode: " + _errorCode);
				}

				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Blog_Posts::Update::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		/// <summary>
		/// Purpose: Delete method. This method will Delete one existing row in the database, based on the Primary Key.
		/// </summary>
		/// <returns>True if succeeded, otherwise an Exception is thrown. </returns>
		/// <remarks>
		/// Properties needed for this method: 
		/// <UL>
		///		 <LI>Identity</LI>
		/// </UL>
		/// Properties set after a succesful call of this method: 
		/// <UL>
		///		 <LI>ErrorCode</LI>
		/// </UL>
		/// </remarks>
		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[Blog_Posts_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@Identity", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _iD));
				cmdToExecute.Parameters.Add(new SqlParameter("@ErrorCode", SqlDbType.Int, 4, ParameterDirection.Output, true, 10, 0, "", DataRowVersion.Proposed, _errorCode));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				_errorCode = (Int32)cmdToExecute.Parameters["@ErrorCode"].Value;

				if(_errorCode != (int)LLBLError.AllOk)
				{
					// Throw error.
					throw new Exception("Stored Procedure 'Blog_Posts_Delete' reported the ErrorCode: " + _errorCode);
				}

				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Blog_Posts::Delete::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		/// <summary>
		/// Purpose: Select method. This method will Select one existing row from the database, based on the Primary Key.
		/// </summary>
		/// <returns>DataTable object if succeeded, otherwise an Exception is thrown. </returns>
		/// <remarks>
		/// Properties needed for this method: 
		/// <UL>
		///		 <LI>Identity</LI>
		/// </UL>
		/// Properties set after a succesful call of this method: 
		/// <UL>
		///		 <LI>ErrorCode</LI>
		///		 <LI>Identity</LI>
		///		 <LI>Title</LI>
		///		 <LI>Body</LI>
		///		 <LI>Published</LI>
		///		 <LI>AllowComments</LI>
		///		 <LI>Syndicate</LI>
		///		 <LI>User_ID</LI>
		///		 <LI>TitleUrl</LI>
		///		 <LI>Source</LI>
		///		 <LI>SourceUrl</LI>
		///		 <LI>Created</LI>
		///		 <LI>Issued</LI>
		///		 <LI>Modified</LI>
		/// </UL>
		/// Will fill all properties corresponding with a field in the table with the value of the row selected.
		/// </remarks>
		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[Blog_Posts_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("Blog_Posts");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@Identity", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _iD));
				cmdToExecute.Parameters.Add(new SqlParameter("@ErrorCode", SqlDbType.Int, 4, ParameterDirection.Output, true, 10, 0, "", DataRowVersion.Proposed, _errorCode));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				_errorCode = (Int32)cmdToExecute.Parameters["@ErrorCode"].Value;

				if(_errorCode != (int)LLBLError.AllOk)
				{
					// Throw error.
					throw new Exception("Stored Procedure 'Blog_Posts_SelectOne' reported the ErrorCode: " + _errorCode);
				}

				if(toReturn.Rows.Count > 0)
				{
					_iD = (Int32)toReturn.Rows[0]["Identity"];
					_title = (string)toReturn.Rows[0]["Title"];
					_body = (string)toReturn.Rows[0]["Body"];
					_published = (bool)toReturn.Rows[0]["Published"];
					_allowComments = (bool)toReturn.Rows[0]["AllowComments"];
					_syndicate = (bool)toReturn.Rows[0]["Syndicate"];
					_user_ID = (Guid)toReturn.Rows[0]["User_ID"];
					_titleUrl = toReturn.Rows[0]["TitleUrl"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["TitleUrl"];
					_source = toReturn.Rows[0]["Source"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Source"];
					_sourceUrl = toReturn.Rows[0]["SourceUrl"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["SourceUrl"];
					_created = (DateTime)toReturn.Rows[0]["Created"];
					_issued = (DateTime)toReturn.Rows[0]["Issued"];
					_modified = (DateTime)toReturn.Rows[0]["Modified"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Blog_Posts::SelectOne::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		/// <summary>
		/// Purpose: SelectAll method. This method will Select all rows from the table.
		/// </summary>
		/// <returns>DataTable object if succeeded, otherwise an Exception is thrown. </returns>
		/// <remarks>
		/// Properties set after a succesful call of this method: 
		/// <UL>
		///		 <LI>ErrorCode</LI>
		/// </UL>
		/// </remarks>
		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[Blog_Posts_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("Blog_Posts");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@ErrorCode", SqlDbType.Int, 4, ParameterDirection.Output, true, 10, 0, "", DataRowVersion.Proposed, _errorCode));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				_errorCode = (Int32)cmdToExecute.Parameters["@ErrorCode"].Value;

				if(_errorCode != (int)LLBLError.AllOk)
				{
					// Throw error.
					throw new Exception("Stored Procedure 'Blog_Posts_SelectAll' reported the ErrorCode: " + _errorCode);
				}

				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Blog_Posts::SelectAll::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlInt32 ID
		{
			get
			{
				return _iD;
			}
			set
			{
				SqlInt32 iDTmp = (SqlInt32)value;
				if(iDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Identity", "Identity can't be NULL");
				}
				_iD = value;
			}
		}


		public SqlString Title
		{
			get
			{
				return _title;
			}
			set
			{
				SqlString titleTmp = (SqlString)value;
				if(titleTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Title", "Title can't be NULL");
				}
				_title = value;
			}
		}


		public SqlString Body
		{
			get
			{
				return _body;
			}
			set
			{
				SqlString bodyTmp = (SqlString)value;
				if(bodyTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Body", "Body can't be NULL");
				}
				_body = value;
			}
		}


		public SqlBoolean Published
		{
			get
			{
				return _published;
			}
			set
			{
				_published = value;
			}
		}


		public SqlBoolean AllowComments
		{
			get
			{
				return _allowComments;
			}
			set
			{
				_allowComments = value;
			}
		}


		public SqlBoolean Syndicate
		{
			get
			{
				return _syndicate;
			}
			set
			{
				_syndicate = value;
			}
		}


		public SqlGuid User_ID
		{
			get
			{
				return _user_ID;
			}
			set
			{
				SqlGuid user_IDTmp = (SqlGuid)value;
				if(user_IDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("User_ID", "User_ID can't be NULL");
				}
				_user_ID = value;
			}
		}


		public SqlString TitleUrl
		{
			get
			{
				return _titleUrl;
			}
			set
			{
				_titleUrl = value;
			}
		}


		public SqlString Source
		{
			get
			{
				return _source;
			}
			set
			{
				_source = value;
			}
		}


		public SqlString SourceUrl
		{
			get
			{
				return _sourceUrl;
			}
			set
			{
				_sourceUrl = value;
			}
		}


		public SqlDateTime Created
		{
			get
			{
				return _created;
			}
			set
			{
				SqlDateTime createdTmp = (SqlDateTime)value;
				if(createdTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Created", "Created can't be NULL");
				}
				_created = value;
			}
		}


		public SqlDateTime Issued
		{
			get
			{
				return _issued;
			}
			set
			{
				SqlDateTime issuedTmp = (SqlDateTime)value;
				if(issuedTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Issued", "Issued can't be NULL");
				}
				_issued = value;
			}
		}


		public SqlDateTime Modified
		{
			get
			{
				return _modified;
			}
			set
			{
				SqlDateTime modifiedTmp = (SqlDateTime)value;
				if(modifiedTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Modified", "Modified can't be NULL");
				}
				_modified = value;
			}
		}
		#endregion
	}
}